const db = require('./db-util')

let sql_selectByRoleId = "SELECT  rp.role_id, p.* FROM demo.role_permission rp\n" +
    "inner join permission p \n" +
    "on p.per_id =rp.permission_id\n" +
    "where rp.role_id =?";

function selectByRoleId(roleId,callback) {
   db.query(sql_selectByRoleId,[roleId],function (err,rows) {

       const result =rows.filter((item)=>{
           return item.super_id ==0;
       })

       const childElements = rows.filter((item)=>{
           return item.super_id!=0;
       })

       for(let index in childElements){
           for(let inner in result)
           if(childElements[index].super_id==result[inner].per_id){
               result[inner].children=[];
               result[inner].children.push(childElements[index])
           }
       }
       callback(result)
   })
}


module.exports={
    selectByRoleId
}
